﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;

namespace Metamorph.Core.Smm
{
	public class StateMachine : IStateMachine
	{
		private string					sId;
		private State					sCurrentState;
		private IProcessContext			smcStateMachineContext;
		private StateMachineDefinition	smdStateMachineDefinition;

		public StateMachine(StateMachineDefinition smdStateMachineDefinition, string sId)
		{
			this.sId						= sId;
			this.smdStateMachineDefinition	= smdStateMachineDefinition;
			sCurrentState					= smdStateMachineDefinition.InitialState;
		}

		#region IStateMachine Members

		public string Id
		{
			get
			{
				return sId;
			}
		}

		public string Name
		{
			get
			{
				return smdStateMachineDefinition.Name;
			}
		}

		public State CurrentState
		{
			get
			{
				return sCurrentState;
			}
			set
			{
				sCurrentState = value;
			}
		}

		public IProcessContext ProcessContext
		{
			get
			{
				return smcStateMachineContext;
			}
			set
			{
				smcStateMachineContext = value;
			}
		}

		public void Reset()
		{
			sCurrentState = smdStateMachineDefinition.InitialState;
		}

		public void Next(string sTransitionName)
		{
			if (sCurrentState.Transitions.ContainsKey(sTransitionName))
			{
				Transition tTransition = sCurrentState.Transitions[sTransitionName];

				if (tTransition.ConditionName != null)
				{
					if (!MetamorphManager.Instance.ConditionDispatcher.Proceed(tTransition.ConditionName, smcStateMachineContext))
					{
						return;
					}
				}

				if (tTransition.ActionName != null)
				{
					MetamorphManager.Instance.ActionDispatcher.Execute(tTransition.ActionName, smcStateMachineContext);
				}
				
				sCurrentState = sCurrentState.Transitions[sTransitionName].Target;

				MetamorphManager.Instance.NotifyChange(this, sCurrentState);
			}
            else
            {
                throw new Exception("The " + sCurrentState.Name + " state does not have a transition named " + sTransitionName + ".");
            }
		}

		public void SetCurrentState(string sStateName)
		{
			if (smdStateMachineDefinition.States.ContainsKey(sStateName))
			{
				sCurrentState = smdStateMachineDefinition.States[sStateName];
			}
			else
			{
				throw new Exception("The state " + sStateName + "does not exist.");
			}
		}

		public bool IsValidTransition(string sTransitionName)
		{
			bool bIsValidTransition = false;

			if (sCurrentState.Transitions.ContainsKey(sTransitionName))
			{
				bIsValidTransition = true;
			}

			return bIsValidTransition;
		}

		#endregion
	}
}
